import { request } from '@/api/service'
import * as api from '@/views/case_module/api'
export const crudOptions = (vm) => {
  return {
    rowHandle: {
      dropdown: { // 操作列折叠
        atLeast: 0, // 至少几个以上的按钮才会被折叠,注意show=false的按钮也会计算在内（行编辑按钮默认是隐藏的也会占一个位置）
        text: '操作', // dropdown按钮文字
        type: 'primary',
        icon: 'el-icon-more'
      },
      width: 120,
      view: {
        disabled () {
          // return false
          return !vm.hasPermissions('Retrieve')
        }
      },
      edit: {
        disabled () {
          // return false
          return !vm.hasPermissions('Update')
        }
      },
      remove: {
        disabled () {
          // return false
          return !vm.hasPermissions('Delete')
        }
      },
      custom: [
        {
          thin: true,
          text: '调试',
          size: 'small',
          type: 'warning',
          icon: 'el-icon-video-play',
          show () {
            // return vm.hasPermissions('PlanTest')
            return true
          },
          emit: 'planTest'
        },
        {
          thin: true,
          text: '查看结果',
          size: 'small',
          icon: 'el-icon-files',
          show () {
            // return vm.hasPermissions('CaseResult')
            return true
          },
          emit: 'showPlanResult'
        }
      ],
      fixed: 'right'
    },
    options: {
      tableType: 'vxe-table',
      rowKey: true, // 必须设置，true or false
      rowId: 'id',
      height: '100%', // 表格高度100%, 使用toolbar必须设置
      highlightCurrentRow: false
    },
    formOptions: { // 表单宽度
      defaultSpan: 24, // 默认的表单 span
      width: '35%'
    },
    indexRow: { // 序号
      title: '序号',
      align: 'center',
      width: 60
    },
    selectionRow: { // 多选框
      align: 'center',
      width: 46
    },
    columns: [
      {
        title: 'Id',
        key: 'id',
        view: { disabled: true },
        form: { // 表单配置
          disabled: true // 禁用表单编辑
        },
        disabled: true, // 隐藏列
        sortable: true
      },
      {
        title: '计划名称',
        key: 'plan_name',
        search: { disabled: false }, // 开启查询
        form: { // 表单配置
          disabled: false, // 禁用表单编辑
          rules: [{ required: true, message: '计划名称' }],
          component: {
            props: {
              clearable: true
            },
            placeholder: '请输入计划名称'
          },
          itemProps: {
            class: { yxtInput: true }
          }
        },
        sortable: false
      },
      {
        title: '运行环境',
        key: 'host_belong',
        type: 'radio',
        width: 150,
        dict: {
          cache: false,
          url: '/test_manage/host_manage',
          value: 'id',
          label: 'host_name'
        }, // 数据字典
        search: { disabled: false }, // 开启查询
        form: { // 表单配置
          disabled: false, // 禁用表单编辑
          rules: [{ required: true, message: '运行环境' }],
          component: {
            span: 24,
            props: {
              multiple: false, clearable: true
            },
            placeholder: '请选择运行环境'
          },
          itemProps: {
            class: { yxtInput: true }
          }
        },
        sortable: false
      },
      {
        title: '所属项目',
        key: 'project_belong',
        type: 'select',
        width: 150,
        dict: {
          cache: false,
          url: '/quality/project/',
          value: 'id',
          label: 'project_name'
        }, // 数据字典
        search: { disabled: false }, // 开启查询
        form: { // 表单配置
          disabled: true, // 禁用表单编辑
          component: {
            props: {
              clearable: true
            },
            placeholder: '请选择所属项目'
          },
          itemProps: {
            class: { yxtInput: true }
          },
          valueChange (key, value, form, { getColumn, mode, component, immediate, getComponent }) {
            form.module_belong = undefined // 将“module_belong”的值置空
            if (value) {
              getComponent('module_belong').reloadDict() // 执行city的select组件的reloadDict()方法，触发“city”重新加载字典
            }
          }
        },
        sortable: false
      },
      {
        title: '所属模块',
        key: 'module_belong',
        type: 'tree-selector',
        width: 150,
        dict: {
          isTree: true,
          label: 'module_name',
          value: 'id',
          cache: false,
          getData: (url, dict, { form, component }) => { // 配置此参数会覆盖全局的getRemoteDictFunc
            return api.ModuleLazy().then(ret => { return ret })
          }
        },
        search: { disabled: false }, // 开启查询
        form: { // 表单配置
          disabled: false, // 禁用表单编辑
          rules: [{ required: true, message: '用例模块' }],
          component: {
            props: {
              multiple: false, clearable: true
            },
            placeholder: '请选择用例模块'
          },
          itemProps: {
            class: { yxtInput: true }
          },
          valueChange (key, value, form, { getColumn, mode, component, immediate, getComponent }) {
            form.cases = undefined // 将“module_belong”的值置空
            if (value) {
              getComponent('cases').reloadDict() // 执行city的select组件的reloadDict()方法，触发“city”重新加载字典
            }
          }
        },
        sortable: false
      },
      {
        title: '包含用例',
        key: 'cases',
        showOverflowTooltip: true,
        search: {
          disabled: true
        },
        minWidth: 250,
        type: 'table-selector',
        dict: {
          cache: false,
          url (dict, { form /* 当前的form表单 */, component /* 当前的组件ref */ }) {
            if (form && form.project_belong != null && form.module_belong != null) { // 本数据字典的url是通过前一个select的选项决定的
              return '/test_manage/case_info/?project_belong=' + form.project_belong + '&module_belong=' + form.module_belong
            } else if (form && form.project_belong != null) {
              return '/test_manage/case_info/?project_belong=' + form.project_belong
            } else if (form && form.module_belong != null) {
              return '/test_manage/case_info/?module_belong=' + form.module_belong
            } else {
              return '/test_manage/case_info/'
            }
          },
          value: 'id', // 数据字典中value字段的属性名
          label: 'case_name', // 数据字典中label字段的属性名
          getData: (url, dict, {
            form,
            component
          }) => {
            return request({
              url: url,
              params: {
                page: 1,
                limit: 10
              }
            }).then(ret => {
              component._elProps.page = ret.data.page
              component._elProps.limit = ret.data.limit
              component._elProps.total = ret.data.total
              return ret.data.data
            })
          }
        },
        form: {
          rules: [ // 表单校验规则
            {
              required: true,
              message: '必填项'
            }
          ],
          itemProps: {
            class: { yxtInput: true }
          },
          component: {
            span: 24,
            pagination: true,
            props: { multiple: true },
            placeholder: '请选择测试用例',
            elProps: {
              columns: [
                {
                  field: 'case_name',
                  title: '接口名称'
                },
                {
                  field: 'url',
                  title: '请求地址'
                },
                {
                  field: 'extract',
                  title: '数据提取规则'
                }
              ]
            }
          }
        }
      },
      {
        title: 'CRON',
        key: 'cron',
        width: 150,
        search: { disabled: true },
        type: 'cron-selector',
        form: {
          rules: [ // 表单校验规则
            {
              required: false,
              message: '必填项'
            }
          ],
          component: {
            placeholder: '可选择时间'
          }
        },
        component: {}
      },
      {
        title: '任务结果通知',
        key: 'result_send_condition',
        type: 'radio',
        dict: {
          data: vm.dictionary('result_send_condition')
        }, // 数据字典
        disabled: true, // 隐藏列
        form: { // 表单配置
          value: 0,
          disabled: false, // 禁用表单编辑
          component: {
            placeholder: '任务结果通知'
          }
        },
        sortable: false
      },
      {
        title: '邮件地址',
        key: 'email_address',
        disabled: true, // 隐藏列
        form: { // 表单配置
          disabled: false, // 禁用表单编辑
          component: {
            show (context) {
              return context.form.result_send_condition !== 0
            },
            placeholder: '请输入邮件地址,多个邮件地址用;分隔'
          }
        },
        sortable: false
      },
      {
        title: '附件地址',
        key: 'file_address',
        disabled: true, // 隐藏列
        form: { // 表单配置
          disabled: false, // 禁用表单编辑
          component: {
            show (context) {
              return context.form.result_send_condition !== 0
            },
            placeholder: '请输入附件地址'
          }
        },
        sortable: false
      },
      {
        title: '第三方推送地址',
        key: 'push_url',
        disabled: true, // 隐藏列
        form: { // 表单配置
          disabled: false, // 禁用表单编辑
          component: {
            show (context) {
              return context.form.result_send_condition !== 0
            },
            placeholder: '请输入第三方推送地址'
          }
        },
        sortable: false
      },
      {
        title: '第三方结果地址',
        key: 'job_url',
        disabled: true, // 隐藏列
        form: { // 表单配置
          disabled: false, // 禁用表单编辑
          component: {
            show (context) {
              return context.form.result_send_condition !== 0
            },
            placeholder: '请输入第三方结果地址'
          }
        },
        sortable: false
      }
    ].concat(vm.commonEndColumns({
      create_datetime: { showTable: false },
      modifier_name: { showTable: true }
    }))
  }
}
